﻿using FactoryDeviceSystem.Controls;
using FactoryDeviceSystem.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FactoryDeviceSystem.Utils
{
    public class ConfigUtil
    {
        public static PLCInfo LoadPlcConfig()
        {
            PLCInfo plcInfo = null;
            string filePath = Application.StartupPath + "\\ConfigFiles\\Communication.json";
            if (File.Exists(filePath))
            {
                string data = File.ReadAllText(filePath);
                plcInfo = JsonConvert.DeserializeObject<PLCInfo>(data);
            }
            return plcInfo;
        }

        public static List<DeviceInfo> LoadDevices()
        {
            List <DeviceInfo> devices = new List <DeviceInfo>();
            string filePath = Application.StartupPath + "\\ConfigFiles\\DeviceSet.json";
            if (File.Exists(filePath))
            {
                string data = File.ReadAllText(filePath);
                devices = JsonConvert.DeserializeObject<List<DeviceInfo>>(data);
            }
            return devices;
        }

        public static List<StoreSet> LoadStores()
        {
            List<StoreSet> storeSets = new List<StoreSet>();
            string filePath = Application.StartupPath + "\\ConfigFiles\\StoreSet.json";
            if (File.Exists(filePath))
            {
                string data = File.ReadAllText(filePath);
                storeSets = JsonConvert.DeserializeObject<List<StoreSet>>(data);
            }

            return storeSets;
        }
    }
}
